package com.tt.fashionWebSite.service.imp;

import com.tt.fashionWebSite.dao.SoDoCoTheDao;
import com.tt.fashionWebSite.dao.ThongTinThanhVienDao;
import com.tt.fashionWebSite.domain.MauSac;
import com.tt.fashionWebSite.domain.SoDoCoThe;
import com.tt.fashionWebSite.dto.*;
import com.tt.fashionWebSite.service.Ad_MemberMeasurementService;
import com.tt.fashionWebSite.service.GenericService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service("ad_memberMeasurementService")
public class Ad_MemberMeasurementServiceImp extends GenericService implements Ad_MemberMeasurementService {
    @Autowired
    @Qualifier("soDoCoThe")
    private SoDoCoTheDao soDoCoTheDao;

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public int adminMemberMeasurement(SoDoCoTheDto memberMeasurementDto) {
        SoDoCoThe soDoCoThe = soDoCoTheDao.findById(memberMeasurementDto.getMaSDCoThe());
        if(soDoCoThe == null){
            soDoCoThe = new SoDoCoThe();
            soDoCoThe.setMaSDCoThe(memberMeasurementDto.getMaSDCoThe());
            soDoCoThe.setSdCanNang(memberMeasurementDto.getSdCanNang());
            soDoCoThe.setSdChieuCao(memberMeasurementDto.getSdChieuCao());
            soDoCoThe.setSdDaiLung(memberMeasurementDto.getSdDaiLung());
            soDoCoThe.setSdDaiTay(memberMeasurementDto.getSdDaiTay());
            soDoCoThe.setSdEo(memberMeasurementDto.getSdEo());
            soDoCoThe.setSdMong(memberMeasurementDto.getSdMong());
            soDoCoThe.setSdNguc(memberMeasurementDto.getSdNguc());
            soDoCoThe.setSdVai(memberMeasurementDto.getSdVai());
            soDoCoTheDao.insert(soDoCoThe);
            return JsonResponseCode.SUCCESS;
        }
        else {
            return JsonResponseCode.FAILED;
        }
    }

    public List<SoDoCoTheDto> getMemberMeasurement () {
        return mapper.mapCollection(soDoCoTheDao.getMemberMeasurement(), SoDoCoTheDto.class);
    }

    public SoDoCoTheDto getMemberMeasurementInfoEdit(SoDoCoTheDto measurementDto){
        SoDoCoThe soDoCoThe = soDoCoTheDao.findById(measurementDto.getMaSDCoThe());
        SoDoCoTheDto soDoCoTheDto = mapper.map(soDoCoThe, SoDoCoTheDto.class,SO_DO_CO_THE_MAP_ID);
        return  soDoCoTheDto;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public SoDoCoTheDto registerMemberMeasurementEdit(SoDoCoTheDto measurementDto) {
        SoDoCoThe soDoCoThe = soDoCoTheDao.findById(measurementDto.getMaSDCoThe());
        try {
            soDoCoThe.setMaSDCoThe(measurementDto.getMaSDCoThe());
            soDoCoThe.setSdNguc(measurementDto.getSdNguc());
            soDoCoThe.setSdMong(measurementDto.getSdMong());
            soDoCoThe.setSdEo(measurementDto.getSdEo());
            soDoCoThe.setSdCanNang(measurementDto.getSdCanNang());
            soDoCoThe.setSdChieuCao(measurementDto.getSdChieuCao());
            soDoCoThe.setSdDaiLung(measurementDto.getSdDaiLung());
            soDoCoThe.setSdDaiTay(measurementDto.getSdDaiTay());
            soDoCoThe.setSdVai(measurementDto.getSdVai());
            soDoCoTheDao.update(soDoCoThe);
            return measurementDto;
        }
        catch (Exception ex) {
            return null;
        }
    }
}